<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Api\Controller;
use Think\Model;
use Api\Logic\UserLogic;

/**
 * 用户维修地址控制器
 * @author derek <693472824@qq.com>
 */
class UserAddressController extends BaseController {
    
    /**
     * 选择维修地址
     * @author derek <693472824@qq.com>
     */
    public function index(){
        
        $data = I();
        
        $userID = $this->checkLogin('UserAddress/index', $data);
        
        //1:来自地址管理；2：来自订单
        $type = I('type');
        $type = $type ? : '1';

        if($type == '2'){
            $this->assign('data', serialize($data));
        }
        
        $addressList = M('UserAddress')->where(array('user_id'=>$userID))->getField('address_id,contact,telphone,address');
        
        $this->assign('type', $type);
        $this->assign('list', $addressList);
        $this->display();
    }
    
    /**
     * 新增或修改地址
     */
    public function editAction(){
        
        $data = I();
        $userID = $this->checkLogin('UserAddress/editAction', $data);

        //腾讯地图回调时返回的参数
        $location = I('latng');
        $address = I('addr');
        $city = I('city');
        $addressID = I('address_id');
        
        if(strpos($location, ',')){
            list($latitude, $longitude) = explode(',', $location);
        }
        
        if (!empty($addressID)) {
        
            $model = M('UserAddress')->find($addressID);
        
            if (empty($model)) {
                $this->error('获取数据失败', U('index'));
            }
        }
        
        if($data['type'] == '2' && !empty($data['data'])){
            session('edit_address_data', $data['data']);
        }
        
        $model['latitude'] = $latitude ? : ($model['latitude'] ? : '');
        $model['longitude'] = $longitude ? : ($model['longitude'] ? : '');
        $model['address'] = $address ? : ($model['address'] ? : '');
        $model['city'] = $city ? : ($model['city'] ? : '');
        
        $this->assign('type', $data['type']);
        $this->assign('data', $data['data']);
        $this->assign('addressModel', $model);
        $this->display('editAction');
    }
    
    /**
     * 保存用户维修地址
     */
    public function saveAction(){
        
        $data = I();
        $userID = $this->checkLogin('UserAddress/editAction', $data);
        
        $type = $data['type'];
        $type = empty($type) ? '1' : intval($type);
        $addressID = $data['address_id'];
        
        $address['user_id'] = $userID;
        $address['contact'] = $data['contact'];
        $address['telphone'] = $data['telphone'];
        $address['sex'] = $data['picker'] == '女' ? '2' : '1';
        $address['city'] = $data['city'];
        $address['address'] = $data['address'];
        $address['name'] = $data['name'];
        $address['latitude'] = $data['latitude'];
        $address['longitude'] = $data['longitude'];
        
        $site = M('Site')->field('site_id,ROUND(6378.137*2*ASIN(SQRT(POW(SIN(('.$address['latitude']
                .'*PI()/180-latitude*PI()/180)/2),2)+COS('.$address['latitude'].'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$address['longitude']
                .'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli')->order('juli')->find();
        
        $address['site_id'] = $site['site_id'];
        
        $addressModel = M('UserAddress');
        
        if($addressModel->create($address)){
            
            if(!empty($addressID)){
                $addressModel->where(array('address_id'=>$addressID))->save();
            }else{
                $addressID = $addressModel->add();
            }
        }
        
        //下订单时新增地址
        if($type == '2'){
            $param = session('edit_address_data');
            session('edit_address_data', null);
            $param = str_replace('&quot;', '"', $param);
            $param = unserialize($param);
            $param['address_id'] = $addressID;
            
            $this->redirect('Orders/appointment', $param);
            
        }else if($type == '1'){
            
            $this->redirect('UserAddress/index', $param);
        }
    }
    
    /**
     * 选择地址后跳转下单页面
     */
    public function order(){
        
        $data['address_id'] = I('address_id');
        $data['type'] = I('type');
        
        $userID = $this->checkLogin('UserAddress/index', 'type='.$data['type']);
        
        if(empty($data['address_id'])){
            $this->error('参数错误', 'Index/index');
        }
        
        $param = I('data');
        $param = str_replace('&quot;', '"', $param);
        $param = unserialize($param);
        $param['address_id'] = $data['address_id'];
        
        $this->redirect('Orders/appointment', $param);
    }
    
    /**
     * 删除地址
     */
    public function delAction(){
        
        $userID = UserLogic::is_login();
        
        if(empty($userID) || !is_numeric($userID)){
            $this->result['status'] = '-201';
        }else{
        
            $addressID = I('address_id');
            
            $map['user_id'] = $userID;
            $map['address_id'] = $addressID;
            
            $result = M('UserAddress')->where($map)->delete();
            
            if($result){
                $this->result['status'] = '1';
            }else{
                $this->result['status'] = '0';
            }
        }
        
        $this->ajaxReturn($this->result);
    }
}
